using System;
using System.Collections.Generic;
using System.Text;
using ProjectBase.Data;
using Clanta.Core.DataInterfaces;
using Clanta.Core.Domain;
using NHibernate.Expression;
using NHibernate;
using System.Collections;

namespace Clanta.Data
{
    public class ListObject
    {
        string text;
        string id;
        string mailAdress;

        public string MailAdress
        {
            get { return mailAdress; }
            set { mailAdress = value; }
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public ListObject(string text)
        {
            this.text = text;
            id = "-1";
        }

        public ListObject(Speaker speaker)
        {
            this.text = speaker.Name;
            this.id = speaker.Id.ToString();
            this.mailAdress = speaker.Email ?? string.Format("Ikke fundet ({0})", speaker.Name);
        }

        public ListObject(Track track)
        {
            this.text = track.Name;
            this.id = track.Id.ToString();
        }

        public ListObject(DateTime datetime)
        {
            this.text = datetime.ToShortDateString();
            this.id = datetime.ToString("yyyy-MM-dd");
        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }
    }

    public class GeneralStatisticsInformation
    {
        public GeneralStatisticsInformation(string sessionFactoryConfigPath)
        {
            session = NHibernateSessionManager.Instance.GetSessionFrom(sessionFactoryConfigPath);
        }

        private ISession session;

        public List<ListObject> GetDaysByConferenceId(long conferenceId)
        {
            IList datoer = session.CreateQuery(
                @"Select distinct p.PresentationStart from Presentation p
                  where p.Conference.Id = :conferenceId
                  order by p.PresentationStart")
                .SetInt64("conferenceId", conferenceId).List();
            List<DateTime> unikke = new List<DateTime>();
            foreach (object data in datoer)
            { 
                if(data != null)
                {
                    DateTime temp = (DateTime)data;
                    if (!unikke.Contains(temp.Date)) unikke.Add(temp.Date);
                }
            }
            List<ListObject> listObjekts = new List<ListObject>();
            listObjekts.Add(new ListObject("All"));
            foreach (DateTime date in unikke)
            { 
            listObjekts.Add(new ListObject(date));
            }

            return listObjekts;
        }
        
        public List<ListObject> GetTracksByConferenceId(long conferenceId)
        {
            List<Track> tracks = session.CreateQuery(
                @"from Track t
                  where t.Conference.Id = :conferenceId
                  order by t.Name")
                .SetInt64("conferenceId", conferenceId)
                .List<Track>() as List<Track>;
            List<ListObject> listObjekts = new List<ListObject>();
            listObjekts.Add(new ListObject("All"));
            foreach (Track track in tracks)
            {
                listObjekts.Add(new ListObject(track));
            }
            return listObjekts;
        }
        
        public List<ListObject> GetSpeakersByConferenceId(long conferenceId)
        {
            List<Speaker> speakers = session.CreateQuery(
                @"from Speaker s
                  where s.Conference.Id = :conferenceId
                  order by s.Name")
                .SetInt64("conferenceId", conferenceId)
                .List<Speaker>() as List<Speaker>;

            List<ListObject> listObjekts = new List<ListObject>();
            listObjekts.Add(new ListObject("All"));
            foreach (Speaker speaker in speakers)
            {
                listObjekts.Add(new ListObject(speaker));
            }
            return listObjekts;
        }

        public List<Presentation> GetPresentationsByCriteria(string speakerId, string trackId, string dateId, long conferenceId, DateTime date)
        {
            StringBuilder presentationQuery = new StringBuilder(@"from Presentation p ");
                presentationQuery.AppendFormat("where p.Conference.Id = {0} ", conferenceId);
                if (speakerId != "-1") presentationQuery.AppendFormat("and exists (from p.Speakers s where s.Id = {0}) ", speakerId);
                if (trackId != "-1") presentationQuery.AppendFormat("and p.Track.Id = {0} ", trackId);
                if (dateId != "-1") presentationQuery.AppendFormat("and p.PresentationStart >= '{0}' and p.PresentationStart < '{1}'", dateId, date.AddDays(1).ToString("yyyy-MM-dd"));
                return session.CreateQuery(presentationQuery.ToString()).List<Presentation>() as List<Presentation>; ;
        }
    }
}
